package com.gewu.pm.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.gewu.pm.R;
import com.gewu.pm.bean.TestBean;

import java.util.ArrayList;
import java.util.List;

import cn.we.swipe.helper.WeSwipe;
import cn.we.swipe.helper.WeSwipeHelper;
import cn.we.swipe.helper.WeSwipeProxyAdapter;

/**
 * 切换账号列表适配器
 */
public class SwitchAccountAdapter extends WeSwipeProxyAdapter<RecyclerView.ViewHolder> {

    private static final int TYPE_ACCOUNT = 0; // 普通账号（可侧滑）
    private static final int TYPE_ADD = 1;     // 添加账号（不可侧滑）

    private List<TestBean> mList = new ArrayList<>();
    private final LayoutInflater mInflater;
    private final Context mContext;
    
    private String mCurrentAccountId; // 当前账号ID
    private OnDeleteListener mDeleteListener;
    private OnAddAccountListener mAddAccountListener;
    private OnItemClickListener mItemClickListener;
    private WeSwipe mWeSwipe;

    public SwitchAccountAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    /**
     * 设置 WeSwipe
     */
    public void setWeSwipe(WeSwipe weSwipe) {
        mWeSwipe = weSwipe;
    }

    /**
     * 设置 Item 点击监听
     */
    public void setOnItemClickListener(OnItemClickListener listener) {
        mItemClickListener = listener;
    }

    /**
     * 设置数据
     */
    public void setData(List<TestBean> data) {
        mList = data;
        proxyNotifyDataSetChanged();
    }

    /**
     * 获取数据
     */
    public List<TestBean> getData() {
        return mList;
    }

    /**
     * 获取某个位置的数据
     */
    public TestBean getItem(int position) {
        return mList.get(position);
    }

    /**
     * 删除指定位置的数据
     */
    public void removeAt(int position) {
        if (position >= 0 && position < mList.size()) {
            mList.remove(position);
            // 删除后需要刷新全部数据，因为最后一项"添加账号"的位置会变化
            proxyNotifyDataSetChanged();
        }
    }

    /**
     * 设置当前账号ID（用于显示对号）
     */
    public void setCurrentAccountId(String accountId) {
        mCurrentAccountId = accountId;
        proxyNotifyDataSetChanged();
    }

    /**
     * 设置删除监听
     */
    public void setOnDeleteListener(OnDeleteListener listener) {
        mDeleteListener = listener;
    }

    /**
     * 设置添加账号监听
     */
    public void setOnAddAccountListener(OnAddAccountListener listener) {
        mAddAccountListener = listener;
    }

    @Override
    public int getItemViewType(int position) {
        // 最后一项是"添加账号"
        if (position == getItemCount() - 1) {
            return TYPE_ADD;
        }
        return TYPE_ACCOUNT;
    }

    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        if (viewType == TYPE_ADD) {
            View view = mInflater.inflate(R.layout.switch_account_add_item, parent, false);
            return new AddViewHolder(view);
        } else {
            View view = mInflater.inflate(R.layout.switch_account_item, parent, false);
            return new AccountViewHolder(view);
        }
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AccountViewHolder) {
            ((AccountViewHolder) holder).bindData(position);
        } else if (holder instanceof AddViewHolder) {
            ((AddViewHolder) holder).bindData();
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    /**
     * 普通账号 ViewHolder（可侧滑删除）
     */
    public class AccountViewHolder extends RecyclerView.ViewHolder 
            implements WeSwipeHelper.SwipeLayoutTypeCallBack {

        private final RelativeLayout mContentLayout;
        private final ImageView mAvatarView;
        private final TextView mAccountNameView;
        private final TextView mAccountInfoView;
        private final ImageView mCheckView;
        private final TextView mDeleteView;

        public AccountViewHolder(View itemView) {
            super(itemView);
            mContentLayout = itemView.findViewById(R.id.rl_content);
            mAvatarView = itemView.findViewById(R.id.iv_avatar);
            mAccountNameView = itemView.findViewById(R.id.tv_account_name);
            mAccountInfoView = itemView.findViewById(R.id.tv_account_info);
            mCheckView = itemView.findViewById(R.id.iv_check);
            mDeleteView = itemView.findViewById(R.id.tv_delete);

            // 设置内容区域点击事件
            mContentLayout.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (mItemClickListener != null && position != RecyclerView.NO_POSITION) {
                    TestBean item = mList.get(position);
                    mItemClickListener.onItemClick(position, item);
                }
            });

            // 设置删除按钮点击事件
            mDeleteView.setOnClickListener(v -> {
                int position = getAdapterPosition();
                if (mDeleteListener != null && position != RecyclerView.NO_POSITION) {
                    TestBean item = mList.get(position);
                    mDeleteListener.onDelete(position, item);
                }
            });
        }

        public void bindData(int position) {
            TestBean item = mList.get(position);
            
            // 显示账号信息
            mAccountNameView.setText(item.getName());
            mAccountInfoView.setText("刘宏伟  销售区域/部门");
            
            // 显示/隐藏选中标记
            if (item.getId() != null && item.getId().equals(mCurrentAccountId)) {
                mCheckView.setVisibility(View.VISIBLE);
            } else {
                mCheckView.setVisibility(View.GONE);
            }
        }

        // 侧滑菜单的宽度
        @Override
        public float getSwipeWidth() {
            return mDeleteView.getWidth();
        }

        // 需要发生滑动的布局
        @Override
        public View needSwipeLayout() {
            return mContentLayout;
        }

        // 未滑动之前展现在屏幕中的布局
        @Override
        public View onScreenView() {
            return mContentLayout;
        }
    }

    /**
     * 添加账号 ViewHolder（不可侧滑）
     */
    public class AddViewHolder extends RecyclerView.ViewHolder 
            implements WeSwipeHelper.SwipeLayoutTypeCallBack {

        private final RelativeLayout mAddLayout;

        public AddViewHolder(View itemView) {
            super(itemView);
            mAddLayout = itemView.findViewById(R.id.rl_add_account);
            
            // 设置整个 Item 的点击事件
            itemView.setOnClickListener(v -> {
                if (mAddAccountListener != null) {
                    mAddAccountListener.onAddAccount();
                }
            });
        }

        public void bindData() {
            // 添加账号项不需要绑定数据
        }

        // 返回 0 表示不可侧滑
        @Override
        public float getSwipeWidth() {
            return 0;
        }

        @Override
        public View needSwipeLayout() {
            return mAddLayout;
        }

        @Override
        public View onScreenView() {
            return mAddLayout;
        }
    }

    /**
     * Item 点击监听接口
     */
    public interface OnItemClickListener {
        void onItemClick(int position, TestBean item);
    }

    /**
     * 删除监听接口
     */
    public interface OnDeleteListener {
        void onDelete(int position, TestBean item);
    }

    /**
     * 添加账号监听接口
     */
    public interface OnAddAccountListener {
        void onAddAccount();
    }
}
